﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;

/// <summary>
/// Author: toanvt
/// CreatedDate: 12/12/2011
/// Description: Inser, Update thông tin hỗ trợ trực tuyến
/// </summary>
public partial class Administrator_AdminSupportIU : System.Web.UI.Page
{

    public Int32 HoTroID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";

            if (Request.QueryString["HoTroID"] != null)
            {
                try
                {
                    HoTroID = Int32.Parse(Request.QueryString["HoTroID"].ToString());
                }
                catch (Exception)
                {
                    HoTroID = -1;
                }
            }

            if (HoTroID == -1)
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                this.lbAdminSupport.Text = "Thêm mới thông tin Hỗ Trợ";

            }
            else
            {
                HoTro theHoTro = HoTroProvider.GetHoTroByHoTroID(HoTroID);

                if (theHoTro == null)
                    Response.Redirect("~/Administrator/AdminSupports.aspx");
                else
                {
                    this.fckGhiChuVN.Value = theHoTro.GhiChuVN.Trim();
                    this.fckGhiChuOther.Value = theHoTro.GhiChuOther.Trim();
                    this.txtNick.Text = theHoTro.Nick.Trim();
                    this.ddlStyle.SelectedValue = theHoTro.Style.Trim();
                    this.txtNumberOfOrder.Text = theHoTro.NumberOfOrder.ToString();
                    this.ckPublish.Checked = theHoTro.Publish;
                    ViewState["ID"] = theHoTro.HoTroID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    this.lbAdminSupport.Text = "Sửa thông tin Hỗ Trợ";
                }
            }

            // ẩn các validation cho các control Other Language
            if (HangSo.USING_MULTIL_LANGUAGE == false)
            {
                rvGhiChuOther.Enabled = false;
            }
        }
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Kiêm tra trạng thái Dữ liệu
    /// </summary>
    /// <returns>trạng thái dữ liệu đúng hay không</returns>
    private Boolean ValidateData()
    {
        //Clear nội dung của Message
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        String message = "";
        Boolean isSuccess = true;

        try
        {

        }
        catch (Exception)
        {

        }

        if (!isSuccess)
        {
            lbMessage.Text = message;
        }

        return isSuccess;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;
        Page.Validate();

        if (Page.IsValid)
        {
            HoTro theHoTro = new HoTro();
            theHoTro.GhiChuVN = this.fckGhiChuVN.Value;
            theHoTro.GhiChuOther = this.fckGhiChuOther.Value;
            theHoTro.Nick = this.txtNick.Text.Trim();
            theHoTro.Style = this.ddlStyle.SelectedValue;
            theHoTro.Publish = this.ckPublish.Checked;

            try
            {
                theHoTro.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theHoTro.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            if (action == HangSo.ACTION_ADD)
            {
                theHoTro.HoTroID = -1;
                HoTroProvider.IUHoTro(theHoTro, true);
            }
            else
            {
                theHoTro.HoTroID = Int32.Parse(ViewState["ID"].ToString());
                HoTroProvider.IUHoTro(theHoTro, false);
            }

            Response.Redirect("~/Administrator/AdminSupports.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminSupports.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;
        Page.Validate();

        if (Page.IsValid)
        {
            HoTro theHoTro = new HoTro();
            theHoTro.GhiChuVN = this.fckGhiChuVN.Value;
            theHoTro.GhiChuOther = this.fckGhiChuOther.Value;
            theHoTro.Nick = this.txtNick.Text.Trim();
            theHoTro.Style = this.ddlStyle.SelectedValue;
            theHoTro.Publish = this.ckPublish.Checked;

            try
            {
                theHoTro.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theHoTro.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            Int32 hoTroIDOut;

            if (action == HangSo.ACTION_ADD)
            {
                theHoTro.HoTroID = -1;
                hoTroIDOut = HoTroProvider.IUHoTro(theHoTro, true);
            }
            else
            {
                theHoTro.HoTroID = Int32.Parse(ViewState["ID"].ToString());
                hoTroIDOut = HoTroProvider.IUHoTro(theHoTro, false);
            }

            Response.Redirect("~/Administrator/AdminSupportIU.aspx?HoTroID=" + hoTroIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
